package com.wisdom.benefit.jufa.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.StrictMode;
import android.text.TextUtils;

import com.ansen.http.entity.HttpConfig;
import com.ansen.http.net.HTTPCaller;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.wisdom.benefit.jufa.R;
import com.wisdom.benefit.jufa.entity.AccountInfo;
import com.wisdom.benefit.jufa.entity.GetStatusInfo;
import com.wisdom.benefit.jufa.event.UserStateEvent;
import com.wisdom.benefit.jufa.net.RetrofitService;
import com.wisdom.benefit.jufa.utils.NotificationCollectorMonitorService;
import com.wisdom.benefit.jufa.utils.PreferencesUtils;
import com.wisdom.benefit.jufa.widget.MyMaterialHeader;

import org.greenrobot.eventbus.EventBus;

import androidx.multidex.MultiDex;


/**
 * Created on 2018/12/05 15:38.
 *
 * @author zhoudawei
 */
public class BaseApplication extends Application {

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.blue_3586f9);
            return new MyMaterialHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    private static BaseApplication mContext;
    private String mToken = "";
    private String userId = "";
    private String versionName = "";
    private String isGoAccount = "";//是否打开自动确认到账功能1-打开0-未打开
    private AccountInfo mAccountInfo;
    private GetStatusInfo getStatusInfo;
    private String time = "";
    private String isInput = "0";
    private String isHaveMa;
    private String isMyLogin;
    private String deviceId;

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            setVersionName(getVersionName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        startService(new Intent(this, NotificationCollectorMonitorService.class));
        HttpConfig httpConfig = new HttpConfig();
        httpConfig.setAgent(true);//有代理的情况能不能访问
        httpConfig.setDebug(true);//是否debug模式 如果是debug模式打印log
        httpConfig.setTagName("ansen");//打印log的tagname
// 初始化MultiDex
        MultiDex.install(this);
        //可以添加一些公共字段 每个接口都会带上
//        httpConfig.addCommonField("pf", "android");
//        httpConfig.addCommonField("version_code", "" + Utils.getVersionCode(getApplicationContext()));

        //初始化HTTPCaller类
        HTTPCaller.getInstance().setHttpConfig(httpConfig);

    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        mContext = this;
    }

    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration configuration = new Configuration();
        configuration.setToDefaults();
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return resources;
    }

    public static BaseApplication getContext() {
        return mContext;
    }

    public String getToken() {
        return mToken == null ? "" : mToken;
    }

    public void setToken(String mToken) {
        this.mToken = mToken;
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(mToken);
    }

    public AccountInfo getAccountInfo() {
        return mAccountInfo;
    }

    public void setAccountInfo(AccountInfo accountInfo) {
        mAccountInfo = accountInfo;
    }

    public GetStatusInfo getGetStatusInfo() {
        return getStatusInfo;
    }

    public void setGetStatusInfo(GetStatusInfo getStatusInfo) {
        this.getStatusInfo = getStatusInfo;
    }

    public String getIsGoAccount() {
        return isGoAccount;
    }

    public void setIsGoAccount(String isGoAccount) {
        this.isGoAccount = isGoAccount;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public static BaseApplication getmContext() {
        return mContext;
    }

    public String getIsInput() {
        return isInput;
    }

    public void setIsInput(String isInput) {
        this.isInput = isInput;
    }

    public static void setmContext(BaseApplication mContext) {
        BaseApplication.mContext = mContext;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }


    public String getIsMyLogin() {
        return isMyLogin;
    }

    public void setIsMyLogin(String isMyLogin) {
        this.isMyLogin = isMyLogin;
    }

    public String getIsHaveMa() {
        return isHaveMa;
    }

    public void setIsHaveMa(String isHaveMa) {
        this.isHaveMa = isHaveMa;
    }

    public String getmToken() {
        return mToken;
    }

    public void setmToken(String mToken) {
        this.mToken = mToken;
    }

    public AccountInfo getmAccountInfo() {
        return mAccountInfo;
    }

    public void setmAccountInfo(AccountInfo mAccountInfo) {
        this.mAccountInfo = mAccountInfo;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getVersionName() throws Exception {
        // 获取packagemanager的实例
        PackageManager packageManager = getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
        String version = packInfo.versionName;
        return version;
    }
}
